package com.bw.controller;

import com.bw.entity.Back;
import com.bw.entity.Day14User;
import com.bw.entity.UserQuery;
import com.bw.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class ListController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/list.do")
    public String list(UserQuery query, Integer pageNum, Model model) {
        //1. 渲染下拉框
        List<Back> backList = userMapper.selectBack();
        model.addAttribute("backList", backList);

        // 2. 分页查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        List<Day14User> list = userMapper.selectByQuery(query);
        PageInfo<Day14User> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);

        // 3. 查询条件不丢失
        model.addAttribute("query",query);
        return "list";
    }
}
